package com.czf.personal.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czf.common.entity.CreditUser;
import com.czf.common.mapper.CreditUserMapper;
import com.czf.common.resp.LoginResponse;
import com.czf.common.util.AppRedisCacheManager;
import com.czf.common.util.CommonUtils;
import com.czf.common.util.UuidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author chenzhanfan
 * @Date 2022/3/16
 * @Description
 */
@Service
@Slf4j
public class UserServiceImpl {

    @Autowired
    private CreditUserMapper creditUserMapper;
    @Autowired
    private UuidGenerator uuidGenerator;
    @Autowired
    private AppRedisCacheManager redisCacheManager;

    public int registerUser(CreditUser user){
        long l = uuidGenerator.nextId();
        user.setUserId(l);
        user.setRegisterTime(CommonUtils.timeStamp());

        QueryWrapper<CreditUser> wrapper = new QueryWrapper<>();
        wrapper.eq("account",user.getAccount());
        wrapper.eq("password",user.getPassword());
        CreditUser creditUser = creditUserMapper.selectOne(wrapper);
        int insertUser = 0;
        if (ObjectUtils.isEmpty(creditUser)) {
            insertUser = creditUserMapper.insert(user);
            log.info("插入是否成功={}",insertUser);
        }
        return insertUser;
    }

    public LoginResponse loginUser(String account,String password){
        CreditUser creditUser = null;
        if (StringUtils.isNotEmpty(account) && StringUtils.isNotEmpty(password)) {
            QueryWrapper<CreditUser> wrapper = new QueryWrapper<>();
            wrapper.eq("account",account);
            wrapper.eq("password",password);
            creditUser = creditUserMapper.selectOne(wrapper);
        }
        if (ObjectUtils.isNotEmpty(creditUser)) {
            // 构建token
            String token = String.valueOf(uuidGenerator.nextId());
            log.info("构建的token值为：{}",token);
            Object tokenValue = redisCacheManager.get(token);
            if (ObjectUtils.isNotEmpty(tokenValue)) {
                redisCacheManager.del(token);
            }
            redisCacheManager.set(token,creditUser.getUserId().toString(),3600);
            LoginResponse response = new LoginResponse();
            response.setUserId(creditUser.getUserId());
            response.setToken(token);
            return response;
        }
        return null;
    }

    public CreditUser getInfo(Long userId){
        Object redisUserInfo = redisCacheManager.get(String.valueOf(userId));
        if (ObjectUtils.isEmpty(redisUserInfo)) {
            CreditUser creditUser = creditUserMapper.selectById(userId);
            redisCacheManager.set(String.valueOf(userId),creditUser,-1);
            return creditUser;
        }
        return (CreditUser) redisUserInfo;
    }

    public int logout(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Object tokenValue = redisCacheManager.get(token);
        if (ObjectUtils.isNotEmpty(tokenValue)) {
            redisCacheManager.del(token);
            return 1;
        }
        return 0;
    }
}
